#!/usr/bin/env python
# -*- coding: utf-8 -*-
import gzip
import io
import urllib2
import re
from pyquery import PyQuery as pq
import json
from bs4 import BeautifulSoup
from StringIO import StringIO

def getHtml(url, date):

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36',
        'Accept':'*/*',
        'Accept-Charset': 'ISO-8859-1,gbk;q=0.7,*;q=0.3',
        'Accept-Encoding': 'gzip,deflate',
        'Connection': 'keep-alive',
        'Referer': 'http://www.szse.cn/main/marketdata/tjsj/jyjg/',

         }
    form={'ACTIONID':'7','AJAX':'AJAX-TRUE','CATALOGID':'1804','TABKEY':'tab1','REPORT_ACTION':'search','txtDate':'2017-02-04'}
    form_data = 'ACTIONID=7&AJAX=AJAX-TRUE&CATALOGID=1804&TABKEY=tab1&REPORT_ACTION=search&txtDate='+ date +''.encode('GBK')
    request = urllib2.Request(url, headers=headers, data=form_data)
    res=  urllib2.urlopen(request)
    the_page = res.read()
    #print the_page
    # bi = io.BytesIO(the_page)
    bi = StringIO(the_page)
    gf = gzip.GzipFile(fileobj=bi, mode="rb")
    #print(gf.read().decode("gbk"))
    page = gf.read().decode("gbk").encode('utf8')
    soup = BeautifulSoup(page, 'html.parser')
    res.close()
    for i in soup.find_all("td"):
        if str(i) == '<td colspan="8" style="text-align:left;background:#ffffff">没有找到符合条件的数据！</td>':
            # print i
            return (None, None)
    #print len(soup.find_all("td"))
    # for i in soup.find_all("td"):
    #     print i
    #filter(str.isdigit, crazystring)
    #print(date)
    # print soup.find_all("td")[19], soup.find_all("td")[59]
    # print int(filter(str.isdigit, str(soup.find_all("td")[19]))[3::]), int(filter(str.isdigit, str(soup.find_all("td")[59]))[3::])
    return (int(filter(str.isdigit, str(soup.find_all("td")[19]))[3::]), int(filter(str.isdigit, str(soup.find_all("td")[59]))[3::]))
    


if __name__ == "__main__":
    date = "2017-02-03"
    data = getHtml("http://www.szse.cn/szseWeb/FrontController.szse?randnum=0.8567524743959484", date)
    print data